﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using Common;
using CNCSoft.EData;

namespace CNCSoft.ErpApps.TMS.longGiaoVu
{
    public partial class frmTask : Form
    {
        SqlConnection conn = new SqlConnection(GlobalVariable.WSInfo.ConnectionString);
        DataSet ds = new DataSet();
        string _userid = GlobalVariable.WSInfo.UserID;

        public frmTask()
        {
            InitializeComponent();
        }

        private void frmTask_Load(object sender, EventArgs e)
        {
            LoadData();
            if (_userid.ToLower() == "tuptc")
            {
                btnThem.Enabled = true;
                btnLuu.Enabled = false;
                btnBoQua.Enabled = false;
            }
            else
            {
                btnThem.Enabled = false;
                btnLuu.Enabled = true;
                btnBoQua.Enabled = true;
            }
        }

        private void frmTask_Enter(object sender, EventArgs e)
        {
            LoadData();
            if (_userid.ToLower() == "tuptc")
                btnThem.Enabled = true;
            else
                btnThem.Enabled = false;
        }

        private string _trangthai = "";
        private void gridView_Data_CellValueChanged(object sender, DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs e)
        {
            try
            {
                _trangthai = gridView_Data.GetFocusedRowCellValue("TrangThai").ToString();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            try
            {
                frmTaskAdd frmAdd = new frmTaskAdd();
                frmAdd.ShowDialog();
                LoadData();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);                
            }
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            UpdateData();
            LoadData();
        }

        private void btnBoQua_Click(object sender, EventArgs e)
        {
            ds.RejectChanges();
            LoadData();
        }

        #region "Coding"
        private void LoadData()
        {
            try
            {
                //Load task to grid
                if (_userid.ToLower() == "tuptc")
                {
                    LoadTask();
                    gridControl_Data.DataSource = ds.Tables["TableTask"];
                }
                else
                {
                    LoadTaskByUser(_userid);
                    gridControl_Data.DataSource = ds.Tables["TableTaskByUser"];
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void LoadTask()
        {
            try
            {
                if (ds.Tables["TableTask"] != null)
                    ds.Tables["TableTask"].Clear();
                string sql = @"usp_AllTask";
                SqlDataAdapter da = new SqlDataAdapter(sql, conn);
                da.Fill(ds, "TableTask");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void LoadTaskByUser(string userid)
        {
            try
            {
                if (ds.Tables["TableTaskByUser"] != null)
                    ds.Tables["TableTaskByUser"].Clear();
                string sql = @"usp_AllTaskByUser @userid = '" + userid + "'";
                SqlDataAdapter da = new SqlDataAdapter(sql, conn);
                da.Fill(ds, "TableTaskByUser");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void UpdateData()
        {
            try
            {
                string sql = @"select * from Task";
                SqlDataAdapter da = new SqlDataAdapter(sql, conn);
                DataHelper.buildDataCommands(ref da);
                da.Update(ds, "TableTaskByUser");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        #endregion
    }
}
